package com.citywy.util;

import com.citywy.utils.TransactionManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("accountAspect")
@Aspect
public class AccountAspect {
    @Autowired
    private TransactionManager tsManager;

    @Pointcut("execution(* com.citywy.service.impl.*.*(..))")
    private void pt1(){}

    @Around("pt1()")
    public Object aroudPrintLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try {
            Object [] args = pjp.getArgs();//得到方法执行所需参数
            //1.开启事务
            tsManager.beginTransaction();
            System.out.println("环绕通知，开始记录日志...前置");
            //2.执行操作
            rtValue=pjp.proceed(args);
            System.out.println("环绕通知，开始记录日志...后置");
            //3.提交事务
            tsManager.commit();
            //4.返回结果
            return rtValue;
        } catch (Throwable t) {
            //5.回滚操作
            System.out.println("环绕通知，开始记录日志...异常");
            tsManager.rollback();
            throw new RuntimeException(t);
        } finally {
            //6.释放连接
            tsManager.release();
            System.out.println("环绕通知，开始记录日志...最终");
        }

    }
}
